angular.module('starter.controllers')
.controller('BigartCtrl', function($scope, museumFac, $stateParams, cfg, $interval, $location, $timeout, $cookies, uploadImg) {

	$scope.width1 = $(window).width();

  $scope.id = $stateParams.id;

	$scope.checkOpenid = function(){
        $scope.url = window.location.href;
        wx.ready(function(){
          wx.onMenuShareTimeline({
            title: $scope.detail.title + '\r\n' + $scope.detail.artist_name, // 分享标题
            link: $scope.url, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
            imgUrl: cfg.baseImgUrl + $scope.detail.work_path[0], // 分享图标
            success: function () { 
                // 用户确认分享后执行的回调函数
            },
            cancel: function () { 
                // 用户取消分享后执行的回调函数
            }
        });
        // 获取“分享给朋友”按钮点击状态及自定义分享内容接口
        wx.onMenuShareAppMessage({
            title: $scope.detail.title,  // 分享标题
            desc: $scope.detail.artist_name, // 分享描述
            link: $scope.url, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
            imgUrl: cfg.baseImgUrl + $scope.detail.work_path[0], // 分享图标
            type: '', // 分享类型,music、video或link，不填默认为link
            dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
            success: function () { 
                // 用户确认分享后执行的回调函数
            },
            cancel: function () { 
                // 用户取消分享后执行的回调函数
            }
          });
        });
	}

	$timeout(function() {
        var mySwiper = new Swiper('.swiper-container',{
          pagination : '.swiper-pagination',
          //pagination : '#swiper-pagination1',
        })
    }, 500);

	  $scope.getLanguage = function(){
    	$scope.language = $cookies.get('language');
    	if($scope.language){
      		$scope.language = $scope.language;
    	}else{
      		$scope.language = 0;
    	}
    	if($scope.language == 0){
	        document.getElementById('title').innerHTML = '提示';
	        document.getElementById('windowclosebutton').value = '确定';
	    }else{
	        document.getElementById('title').innerHTML = 'Hint';
	        document.getElementById('windowclosebutton').value = 'Confirm';
	    }

      if($scope.language==0){
        switch($stateParams.id){
          case '<37': $scope.viewtitle = '作品详情';break;
          case '>36': $scope.viewtitle = '建筑详情';break;
          default: $scope.viewtitle = '作品详情';return;
        }
      }else{
        switch($stateParams.id){
          case '<37': $scope.viewtitle = 'Artwork Details';break;
          case '>36': $scope.viewtitle = 'Detail';break;
          default: $scope.viewtitle = 'Artwork Details';return;
        }
      }

  	}
    $scope.getLanguage();

  	// 每1秒获取进度条
    $scope.pinglunrush = function(){
    	
    		  $interval(function() {
    		  	if($scope.w2 != '100%'){
			        $scope.w = $('.audioplayer-bar-loaded').width();
			        $scope.w2 = $scope.w/$scope.width1*100+'%';
			        console.log($scope.w2);
		        }
		      }, 100);
    	
    }

    // $scope.pinglunrush();

    $scope.procressinnit = function(){
    	$timeout(function() {
			$scope.w2 = '25%';
		}, 1000);

    	$timeout(function() {
    		$scope.w2 = '50%';
    	}, 2000)

    	$timeout(function() {
    		$scope.w2 = '75%';
    	}, 3000)

    	$timeout(function() {
    		$scope.w2 = '100%';
    	}, 4000)
    }

  	$scope.getDetail = function(){
  		$scope.w2 = 0;
		museumFac.info.workdetail({
			workId:$stateParams.id
		},function(data){
        $scope.subscribe = data.subscribe;
	        $scope.detail = data.info;
          $scope.alllength = data.info.work_path.length;
          // console.log($scope.alllength);
	        $scope.checkOpenid();

	        $scope.ao = $scope.detail.audio_path.length;
	        $scope.ao1 = $scope.detail.audio_en_path.length;


	        // (function(doc){var addEvent='addEventListener',type='gesturestart',qsa='querySelectorAll',scales=[1,1],meta=qsa in doc?doc[qsa]('meta[name=viewport]'):[];function fix(){meta.content='width=device-width,minimum-scale='+scales[0]+',maximum-scale='+scales[1];doc.removeEventListener(type,fix,true);}if((meta=meta[meta.length-1])&&addEvent in doc){fix();scales=[.25,1.6];doc[addEvent](type,fix,true);}}(document));

	        // 中文
	        if($scope.language == 0){

	        	if($scope.ao>20){
	        		(function(doc){var addEvent='addEventListener',type='gesturestart',qsa='querySelectorAll',scales=[1,1],meta=qsa in doc?doc[qsa]('meta[name=viewport]'):[];function fix(){meta.content='width=device-width,minimum-scale='+scales[0]+',maximum-scale='+scales[1];doc.removeEventListener(type,fix,true);}if((meta=meta[meta.length-1])&&addEvent in doc){fix();scales=[.25,1.6];doc[addEvent](type,fix,true);}}(document));
		        	document.getElementById("music1").src = cfg.baseImgUrl+$scope.detail.audio_path;//"http://store.sifangartmuseum.org.cn/Uploads/Download/2017-11-07/5a0172f00db88.mp3";
		        	$( function() { $( 'audio' ).audioPlayer(); } );
		        	// $scope.play_mark = 1;
		        	
		        	// $scope.playAudio();
		        	// $timeout(function() {
		        	// 	$scope.w = $('.audioplayer-bar-loaded').width();
		        	// 	alert($scope.w);
		        	// 	$('.audioplayer-playpause').trigger("click");
		        	// 	$scope.procressinnit();
		        	// }, 20);
		        	
		        	
		        }

	        }

	        // 英文
	        if($scope.language == 1){

	        	if($scope.ao1>20){
	        		(function(doc){var addEvent='addEventListener',type='gesturestart',qsa='querySelectorAll',scales=[1,1],meta=qsa in doc?doc[qsa]('meta[name=viewport]'):[];function fix(){meta.content='width=device-width,minimum-scale='+scales[0]+',maximum-scale='+scales[1];doc.removeEventListener(type,fix,true);}if((meta=meta[meta.length-1])&&addEvent in doc){fix();scales=[.25,1.6];doc[addEvent](type,fix,true);}}(document));
		        	document.getElementById("music1").src = cfg.baseImgUrl+$scope.detail.audio_en_path;
		        	//"http://store.sifangartmuseum.org.cn/Uploads/Download/2017-11-01/59f982754792f.mp3";
		        	$( function() { $( 'audio' ).audioPlayer(); } );

		        	// $scope.play_mark = 1;
		        	
		        	// $scope.playAudio();
		        	// $timeout(function() {
		        	// 	$scope.w = $('.audioplayer-bar-loaded').width();
		        	// 	alert($scope.w);
		        	// 	$('.audioplayer-playpause').trigger("click");
		        	// 	$scope.procressinnit();
		        	// }, 20);
		        }

	        }

	      },function(data){
	        alert(data.data.info);
	    });
	}
	$scope.getDetail();

	$scope.gotoList = function(){
		$location.url('artlist/' + $scope.detail.category);
	}

	$scope.change_language = function(id){
      $scope.language = id;
      var expireDate = new Date();  
      expireDate.setDate(expireDate.getDate() + 30);                
      $cookies.put('language', id, {'expires': expireDate.toUTCString()});
      location.reload();
    }

    $scope.gotoMap = function(){
    	window.open('http://wxb7b438d8ca6c77e5.pro.brtbeacon.com.cn/nav');
  	}

    $scope.gotoMap2 = function(){
      var a = 'http://store.sifangartmuseum.org.cn/Public/museum/www/img/map.jpg';
      var tmp = ['http://store.sifangartmuseum.org.cn/Public/museum/www/img/map.jpg'];
      uploadImg.previewImg(a, tmp);
    }
})